//
//  ViewController.m
//  CHENGER
//
//  Created by mac on 16/11/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ViewController.h"
#import "People.h"
#import "DatabaseTool.h"
#import "ViewController1.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (retain,nonatomic)UITableView *tableView;
@property(nonatomic,copy)NSMutableArray *contactArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@",NSHomeDirectory());
    self.view.backgroundColor=[UIColor redColor];
    self.automaticallyAdjustsScrollViewInsets=NO;
    [DatabaseTool opendataandcreatetable];
    _contactArray=[[NSMutableArray alloc]init];
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, 375, 667-64)];

    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.rowHeight=50;
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];
    [self.view addSubview:_tableView];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_contactArray setArray:[DatabaseTool getallcontact]];
    NSLog(@">>>%ld",_contactArray.count);
    [_tableView reloadData];

}
-(void)add{
    ViewController1 *vc1=[[ViewController1 alloc]init];

    [self.navigationController pushViewController:vc1 animated:YES];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _contactArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    People *p=_contactArray[indexPath.row];
    cell.textLabel.text=p.name;

    return cell;
}
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    ViewController1 *vc1=[[ViewController1 alloc]init];

    People *pp=_contactArray[indexPath.row ];

    vc1.person=pp;
    [self.navigationController pushViewController:vc1 animated:YES];


}
-(void)dealloc{

    [_tableView release];
    [_contactArray release];
    [super dealloc];

}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
